<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/10
 * Time: 21:46
 */

namespace app\admin\controller;


use app\admin\model\CityRange;
use app\admin\model\ClassType;
use app\admin\model\DriverType;
use app\admin\model\Tips;
use think\Controller;

class Publiccon extends Controller
{
    //驾照类型
    public function driverCode(){
        return $this->fetch();
    }
    public function driverCodeData($page,$limit){
        $data=DriverType::page($page,$limit)->select()->toArray();
        $count=DriverType::count();
        return json(['code'=>0,'msg'=>'返回成功','count'=>$count,'data'=>$data]);
    }
    //添加驾照类型
    public function driverAdd(){
        if(request()->post()){
            $post=input('post.');
            $data=[
                'name'=>$post['name'],
                'driver_num'=>$post['driver_num'],
                'sort'=>$post['sort'],
            ];
            $save=DriverType::insert($data);
            if($save){
                return json(['code'=>1,'msg'=>'提交成功！']);
            }else{
                return json(['code'=>0,'msg'=>'提交失败！']);
            }
        }
        return $this->fetch();
    }
    //修改驾照类型
    public function driverEdit(){
        if(request()->post()){
            $post=input('post.');
            $data=[
                'name'=>$post['name'],
                'driver_num'=>$post['driver_num'],
                'sort'=>$post['sort'],
            ];
            $save=DriverType::where('id',$post['id'])->update($data);
            if($save){
                return json(['code'=>1,'msg'=>'提交成功！']);
            }else{
                return json(['code'=>0,'msg'=>'提交失败！']);
            }
        }
        $data=DriverType::where('id',input('get.id'))->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    //删除驾照类型
    public function driverDel(){
        $post=input('post.');
        $del=DriverType::destroy($post['id']);
        if($del){
            return json(['code'=>1,'msg'=>'删除成功！']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败！']);
        }
    }
    //驾照类型
    public function classType(){
        return $this->fetch();
    }
    public function classTypeData($page,$limit){
        $data=ClassType::page($page,$limit)->select()->toArray();
        foreach($data as $k=>$v){
            $v['class_type']=explode(',',$v['class_type']);
            if(in_array(1,$v['class_type'])){
                $class_type0="*预约练车*";
            }else{
                $class_type0="";
            }
            if(in_array(2,$v['class_type'])){
                $class_type1="*补考缴费*";
            }else{
                $class_type1="";
            }
            $data[$k]['class_type']=$class_type0.$class_type1;
        }
        $count=ClassType::count();
        return json(['code'=>0,'msg'=>'返回成功','count'=>$count,'data'=>$data]);
    }
    //添加驾照类型
    public function classTypeAdd(){
        if(request()->post()){
            $post=input('post.');
            $data=[
                'class_name'=>$post['class_name'],
                'class_type'=>implode(',',$post['class_type']),
                'sort'=>$post['sort'],
            ];
            $save=ClassType::insert($data);
            if($save){
                return json(['code'=>1,'msg'=>'提交成功！']);
            }else{
                return json(['code'=>0,'msg'=>'提交失败！']);
            }
        }
        return $this->fetch();
    }
    //修改驾照类型
    public function classTypeEdit(){
        if(request()->post()){
            $post=input('post.');
            $data=[
                'class_name'=>$post['class_name'],
                'class_type'=>implode(',',$post['class_type']),
                'sort'=>$post['sort'],
            ];
            $save=ClassType::where('id',$post['id'])->update($data);
            if($save){
                return json(['code'=>1,'msg'=>'提交成功！']);
            }else{
                return json(['code'=>0,'msg'=>'提交失败！']);
            }
        }
        $data=ClassType::where('id',input('get.id'))->find();
        $data['class_type']=explode(',',$data['class_type']);
        $class_type=array();
        if(in_array(1,$data['class_type'])){
            $class_type[0]=1;
        }else{
            $class_type[0]=0;
        }
        if(in_array(2,$data['class_type'])){
            $class_type[1]=2;
        }else{
            $class_type[1]=0;
        }
        $data['class_type']=$class_type;
        $this->assign('data',$data);
        return $this->fetch();
    }
    //删除驾照类型
    public function classTypeDel(){
        $post=input('post.');
        $del=ClassType::destroy($post['id']);
        if($del){
            return json(['code'=>1,'msg'=>'删除成功！']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败！']);
        }
    }
    //驾照类型
    public function city(){
        return $this->fetch();
    }
    public function cityData($page,$limit){
        $data=CityRange::page($page,$limit)->select()->toArray();
        $count=CityRange::count();
        return json(['code'=>0,'msg'=>'返回成功','count'=>$count,'data'=>$data]);
    }
    //添加驾照类型
    public function cityAdd(){
        if(request()->post()){
            $post=input('post.');
            $data=[
                'name'=>$post['name'],
                'sort'=>$post['sort'],
            ];
            $save=CityRange::insert($data);
            if($save){
                return json(['code'=>1,'msg'=>'提交成功！']);
            }else{
                return json(['code'=>0,'msg'=>'提交失败！']);
            }
        }
        return $this->fetch();
    }
    //修改驾照类型
    public function cityEdit(){
        if(request()->post()){
            $post=input('post.');
            $data=[
                'name'=>$post['name'],
                'sort'=>$post['sort'],
            ];
            $save=CityRange::where('id',$post['id'])->update($data);
            if($save){
                return json(['code'=>1,'msg'=>'提交成功！']);
            }else{
                return json(['code'=>0,'msg'=>'提交失败！']);
            }
        }
        $data=CityRange::where('id',input('get.id'))->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    //删除驾照类型
    public function cityDel(){
        $post=input('post.');
        $del=CityRange::destroy($post['id']);
        if($del){
            return json(['code'=>1,'msg'=>'删除成功！']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败！']);
        }
    }



    public function tips(){
        if ($this->request->isPost()){
            $post = input('post.');
            $data = ['content'=>$post['note']];
            $ret = Tips::update($data,['id'=>1]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'提交失败！']);
            }
            return json(['code'=>1,'msg'=>'提交成功！']);
        }
        $data = Tips::where('id',1)->value('content');
        $this->assign('data',$data);
        return $this->fetch();
    }

}